<template>
  <div id="app">
    <!-- 导航栏 -->
    <nav class="nav">
      <ul class="nav-list">
        <li class="active">首页</li>
        <button @click="goToVideoCenter">视频中心</button> <!-- 修改按钮点击事件 -->
        <button @click="goToSubmit">创作中心</button>
        <button @click="goToCheck">审核中心</button>
        <button @click="goToAIChat">智能问答</button>
      </ul>
      <button class="user-icon">
        <img src="../assets/yonghu.png" style="width: 37px; height: 37px; position: absolute; top: 1px; left:3px;" />
      </button>
    </nav>

    <!-- 轮播图区域 -->
    <section
      class="banner-section"
      :style="{
        backgroundImage: `url(${
          bannerList[currentIndex] ? bannerList[currentIndex].coverImage : ''
        })`,}"
    >
      <div class="banner-main">
        <div v-if="bannerList[currentIndex]" class="main-item">
          <div class="banner-text-overlay">
            <h2>{{ bannerList[currentIndex].title }}</h2>
            <div class="bottom-row">
              <p>保护农业文化遗产，弘扬中国传统文化</p>
              <div class="author-info">朝九晚五 | 7777</div>
            </div>
          </div>
        </div>
      </div>
      <div class="banner-thumb">
        <div
          class="thumb-item"
          v-for="(item, index) in bannerList"
          :key="index"
          :class="{ active: currentIndex === index }"
          @click="currentIndex = index"
        >
          <img
            :src="item.coverImage"
            alt="Thumbnail Image"
            style="width: 100%; height: 100%"
          />
        </div>
      </div>
    </section>

    <!-- 推荐模块 -->
    <section class="recommend-section">
      <h3>为你推荐</h3>
      <div class="recommend-ctrl">
        <button @click="prevPage" :disabled="pageNum === 1">←</button>
        <button @click="nextPage">→</button>
      </div>
      <div class="recommend-list">
        <div
          class="recommend-item"
          v-for="(item, index) in displayRecommendList"
          :key="index"
        >
          <img
            :src="item.coverImage"
            alt="Video Cover"
            class="item-video"
            style="width: 100%; height: 200px; object-fit: cover"
          />
          <p class="duration">{{ item.duration }}</p>
          <h4>{{ item.title }}</h4>
          <div class="item-meta">
            <span>{{ item.author }}</span>
            <span>{{ item.viewCount }}</span>
          </div>
        </div>
      </div>
    </section>
  </div>
</template>

<script setup>
import { ref, onMounted } from "vue";
import axios from "../axios/index.js";
import { useRouter } from 'vue-router';

const router = useRouter();

const goToSubmit = () => {
  router.push({ name: 'submitCenter' });
};

const goToCheck = () => {
  router.push({ name: 'checkCenter' });
};

const goToAIChat = () => {
  router.push({ name: 'AIchat' });
};

const goToVideoCenter = () => {
  router.push({ name: 'VideoCenter' }); // 添加新的跳转函数
};

// 轮播图数据
const bannerList = ref([]);
const currentIndex = ref(0);

// 推荐数据
const recommendData = ref([]);
const pageNum = ref(1);
const pageSize = 4;

const displayRecommendList = ref([]);

const prevPage = () => {
  if (pageNum.value > 1) {
    pageNum.value--;
    updateDisplayList();
  }
};

const nextPage = () => {
  const totalPage = Math.ceil(recommendData.value.length / pageSize);
  if (pageNum.value < totalPage) {
    pageNum.value++;
    updateDisplayList();
  }
};

const updateDisplayList = () => {
  const start = (pageNum.value - 1) * pageSize;
  const end = start + pageSize;
  displayRecommendList.value = recommendData.value.slice(start, end);
};

let intervalId;

const startAutoPlay = () => {
  intervalId = setInterval(() => {
    currentIndex.value = (currentIndex.value + 1) % bannerList.value.length;
  }, 3000);
};

const stopAutoPlay = () => {
  clearInterval(intervalId);
};

onMounted(async () => {
  try {
    const [bannerRes, recommendRes] = await Promise.all([
      axios.get("/video-list"),
      axios.get("/recommend-list"),
    ]);
    bannerList.value = bannerRes.data;
    recommendData.value = recommendRes.data;
    updateDisplayList();
    startAutoPlay();
  } catch (error) {
    console.error("Error fetching data:", error);
  }
});
</script>

<style scoped>
/* 全局样式 */
#app {
  margin: 0 auto;
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
  padding-top: 60px;
  padding-left: 0;
}
* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}
/* 导航栏样式 */
.nav {
  display: flex;
  align-items: center;
  background: #f8f9fa;
  box-sizing: border-box;
  padding: 0 20px;
  height: 60px;
  border-bottom: 1px solid #e9ecef;
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  z-index: 1000;
  justify-content: center;
}
.nav-list {
  display: flex;
  list-style: none;
  margin: 0;
  padding: 0;
  justify-content: space-around;
  width: 60%;
  max-width: 800px;
  align-items: center;
}
.nav-list button {
    padding: 10px 15px;
}
.nav-list li.active {
    cursor: pointer;
    color: #495057;
    color: white;
    background: #007bff;
    padding: 20px 15px;
    border-radius: 4px;
    font-weight: bold;
    width: 100px;
}
.user-icon {
  width: 45px;
  height: 45px;
  background: #dee2e6;
  border-radius: 50%;
  position: absolute;
  right: 50px;
}

/* 轮播图样式 */
.banner-section {
  color: white;
  flex: 1;
  position: relative;
  left:20px;
  margin: 0;
  padding: 0;
  background-size: cover;
  background-position: center;
}
.banner-main {
  height: 100%;
  box-sizing: border-box;
  width: 100%;
  position: relative;
  left: -50;
  margin: 0;
  padding: 0;
  height: 438px;
}
.main-item {
  position: relative;
  height: 100%;
}
.main-item img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
}
.banner-text-overlay {
  position: relative;
  top:455px;
  width: 100%;
  background: linear-gradient(0deg, rgba(0, 0, 0, 0.8) 0%, rgba(0, 0, 0, 0) 100%);
  color: white;
  z-index: 1;
  padding: 20px 20px;
  box-sizing: border-box;
}
.banner-text-overlay h2 {
  font-size: 28px;
  margin: 0 0 10px 0;
  display: flex;
  position: relative;
}
.bottom-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.banner-text-overlay p {
  font-size: 16px;
  margin: 0;
}
.banner-text-overlay .author-info {
  font-size: 16px;
  margin: 0;
}
.banner-thumb {
  display: flex;
  gap: 10px;
  position: relative;
  top: 154px;
  left: 8%;
  padding: 15px;
  background-color: rgba(100, 100, 100, 0.5);
  width: 1000px;
}
.thumb-item {
  width: 200px;
  height: 100px;
  background: #ddd;
  border: 2px solid transparent;
  transition: all 0.3s;
}
.thumb-item.active {
  border-color: blue;
}
/* 推荐模块样式 */
.recommend-section {
  display: fixed;
  position: relative;
  top: 200px;
  left: 25px;
  padding: 40px 20px;
  flex: 1;
}
.recommend-ctrl {
  display: flex;
  justify-content: center;
  gap: 20px;
  margin-bottom: 20px;
}
.recommend-list {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
  gap: 20px;
  position: relative;
  left: 115px;
}
.recommend-item {
  border: 1px solid #dee2e6;
  padding: 15px;
}
.duration {
  display: block;
  text-align: right;
  color: #6c757d;
  font-size: 14px;
}
.item-meta {
  font-size: 12px;
  color: #6c757d;
  display: flex;
  justify-content: space-between;
  margin-top: 5px;
}
</style>